package com.hospital.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.domain.pojo.Patient;
import com.hospital.domain.utils.Result;
import com.hospital.mapper.PatientMapper;
import com.hospital.service.PatientService;
import com.hospital.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    private final UserService userService;
    @Override
    public Result signupPatient(String name, String cardId, String phone, String pw, Integer state) {
        if(lambdaQuery().eq(Patient::getPCardId, cardId).exists()){
            return Result.fail("每个用户只能注册一个账号");
        }
        if(lambdaQuery().eq(Patient::getPPhone, phone).exists()){
            return Result.fail("该电话号码已被使用");
        }
        save(new Patient(name, cardId, phone, pw, state));
        Patient patient = lambdaQuery().eq(Patient::getPCardId, cardId).one();
        return userService.signup(phone, pw, 0, 1, patient.getPId());
    }
}
